<template>
  <div>
    <my-breadcrumb :items="['系统管理', '广告页']"/>
    <search :formItems="searchItems"  @search='getList' ref='search' />
    <div class="add">
      <el-button type="primary" @click="toAdd">新增广告页</el-button>
    </div>
    <el-table :data="ads">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="发布时间"
        prop="updatetime">
      </el-table-column>
      <el-table-column
        label="展示时间"
        prop="showtime">
      </el-table-column>
      <el-table-column
        label="展示顺位"
        prop="orderby">
      </el-table-column>
      <el-table-column
        label="图片"
        prop="img">
        <template slot-scope="scope">
          <img :src="scope.row.img" alt="" height="40">
        </template>
      </el-table-column>
      <el-table-column
        label="广告页状态"
        prop="status">
        <template slot-scope="scope">
          {{ scope.row.status === '1' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        prop="id">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="isAdd ? '新增广告页' : '信息修改'" :visible.sync="showAdd">
      <el-form :model="ad" label-width="70px" ref="addForm">
        <el-form-item label="是否启用" prop="status">
          <el-select v-model="ad.status">
            <el-option value="1" label="是"/>
            <el-option value="0" label="否"/>
          </el-select>
        </el-form-item>
        <el-form-item label="展示时间（秒）" prop="showtime" label-width="110px">
          <el-input-number v-model="ad.showtime" :min="0"/>
        </el-form-item>
        <el-form-item label="展示顺位" prop="orderby">
          <el-input-number v-model="ad.orderby" :min="0"/>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <img v-if="ad.img" :src="ad.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
    <select-list ref="selectNew" :url="'/placard/index?delflag=0'" :searchItems="newSearch" :tableItem="newTableItems" @selectRow="nowChange"/>
    <select-list ref="selectTopic" :url="'/article/index?delflag=0'" :searchItems="topicSearch" :tableItem="topicTableItems" @selectRow="nowChange"/>
    <select-list ref="selectRepo" :url="'/repo/index?delflag=0'" :searchItems="repoSearch" :tableItem="repoTableItems" @selectRow="nowChange"/>
    <select-list ref="selectShop" :url="'/Used/index?delflag=0'" :searchItems="shopSearch" :tableItem="shopTableItems" @selectRow="nowChange"/>
  </div>
</template>

<script>
import {deleteRow} from '../../utils/getList'
import selectList from '@/components/common/selectList'
import tableItems from '@/config/tableItems'
export default {
  name: 'ad',
  data () {
    return {
      searchItems: [
        {
          label: '展示时间',
          type: 'input',
          key: 'showtime'
        },
        {
          label: '展示顺位',
          type: 'input',
          key: 'orderby'
        },
        {
          label: '广告页状态',
          type: 'select',
          key: 'status',
          options: [{ id: '0', name: '否' }, { id: '1', name: '是' }],
          width: '100'
        }
      ], // 检索结构
      newSearch: [
        {
          label: '标题',
          type: 'input',
          key: 'title'
        },
        {
          label: '商品名称',
          type: 'input',
          key: 'name'
        }
      ],
      topicSearch: [
        {
          label: '帖子标题',
          type: 'input',
          key: 'title'
        },
        {
          label: '发布人',
          type: 'input',
          key: 'membername'
        }
      ],
      repoSearch: [
        {
          label: '标题',
          type: 'input',
          key: 'title'
        }
      ],
      shopSearch: [
        {
          label: '标题',
          type: 'input',
          key: 'title'
        },
        {
          label: '商品名称',
          type: 'input',
          key: 'skirtname'
        },
        {
          label: '发布人',
          type: 'input',
          key: 'membername'
        }
      ],
      selectNow: {},
      newTableItems: tableItems.newList,
      topicTableItems: tableItems.postList,
      repoTableItems: tableItems.repoList,
      shopTableItems: tableItems.shopList,
      ads: [],
      showAdd: false,
      isAdd: true,
      ad: {
        status: '1',
        showtime: 0,
        img: '',
        orderby: 0
      },
      updateRow: null
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {
        ...searchData
      }
      this.axios.get('/Config/getadvertisement', { params })
        .then(res => {
          this.ads = res.data.data.list
        })
    },

    beforeUpload (file) {
      let reader = new FileReader()
      let _self = this
      reader.onload = function (ev) {
        _self.ad.img = ev.currentTarget.result
      }
      reader.readAsDataURL(file)
      return false
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      this.$nextTick(() => {
        Object.keys(this.ad).forEach(key => {
          this.ad[key] = row[key]
        })
      })
    },
    toSubmit () {
      let url = '/Config/addadvertisement'
      let params = {}
      Object.keys(this.ad).forEach(key => {
        params[key] = this.ad[key]
      })
      if (!this.isAdd) {
        url = '/Config/editadvertisement'
        params.id = this.updateRow.id
      }
      this.axios.post(url, this.qs.stringify(params))
        .then(res => {
          if (res.data.status === '1' || res.data.status === 1) {
            this.getList()
            this.$message.success(res.data.msg)
            this.showAdd = false
          }
        })
    },
    toDelete (id) {
      deleteRow({url: '/Config/deladvertisement', id}, res => {
        if (res.data.status === '1' || res.data.status === 1) {
          this.getList()
          this.$message.success(res.data.msg)
        }
      })
    },
    toSelect (ref) {
      this.$refs[ref].toShow()
      this.selectNow = {}
      this.banner.jumpid = ''
    },
    nowChange (row) {
      this.selectNow = row
      this.banner.jumpid = row.id
    }
  },
  created () {
    this.getList()
  },
  components: {
    selectList
  }
}
</script>

<style scoped>
  .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
